import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Console;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;

import java.util.*;
public class driver{
	static String folderlocation;
	public static String choiceAccessionParse(String folderlocation,BufferedReader in) throws Throwable{
		String filename;
		AccessionParse AP;
		int temporaryinteger;
		int tempint2;
		String outputfilename;
		System.out.println("output filename? ");
		outputfilename = in.readLine();
		AP = new AccessionParse(folderlocation + outputfilename);
		System.out.println("Accession List filename?");
		filename = in.readLine();
		
		System.out.println("choose database or 3 to input name");
		System.out.println("1. IPI_Human_20060712.fasta");
		System.out.println("2. NCBInr_20080418.fasta");
		System.out.println("3. File Contains appropriate peptide information");
		System.out.println("4. input database filename");
		temporaryinteger = Integer.valueOf(in.readLine());
		if(temporaryinteger != 3){
			AP.displaySample(folderlocation + filename, "\\s", 1);
			System.out.println("pertinent column?");
			temporaryinteger = Integer.valueOf(in.readLine());
			AP.accessionGenerateColumn(folderlocation + filename, temporaryinteger, "\\s");	
		}
		switch(temporaryinteger){
		
		case 1:
			AP.grabSequences(folderlocation+ "IPI_Human_20060712.fasta", true);
			AP.finalize();
			break;
		case 2:
			AP.grabSequences("D:/Z drive backup/Databases/NCBInr_20080418.fasta", true);
			AP.finalize();
			break;
		case 3:
			AP.displaySample(folderlocation + filename, "\\s" , 5);
			System.out.println("pertinent column accessionnumber? ");
			temporaryinteger = Integer.valueOf(in.readLine());
			AP.grabInternalSequences(folderlocation+filename, temporaryinteger, "\\s");
			AP.finalize();
			break;
		case 4: 
			System.out.println("filename of database?");
			filename = in.readLine();
			AP.grabSequences(folderlocation + filename , true);
			AP.finalize();
			break;
		}
		return outputfilename;				
	}
	public static void main(String[] args) throws Throwable{
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String input = "";

		fastaCombine FC = null;
		RetCalc RC;
		
		folderlocation = "maldi-excluder/trunk/files/";
		
		int choice = 0;
		String outputfilename = null;
		int temporaryinteger;
		while(choice != 8){
			System.out.println("1. AccessionParse");
			System.out.println("2. Fasta");
			System.out.println("8. exit");
			choice = Integer.valueOf(in.readLine());
			switch (choice){
			case 1:
				outputfilename = choiceAccessionParse(folderlocation, in);
				break;
			case 2:
				if(outputfilename == null){
					System.out.println("Enter fastafile");
					outputfilename = in.readLine();
				}
				System.out.println("Enter split rules");
				input = in.readLine();
			
				runfasta(FC, outputfilename , input);		
				break;
			case 3:
				
				break;
				
			case 4:
				choice = 8;
				break;
			}
			
		}

	}
	private static void runfasta(fastaCombine FC, String outputfilename, String splitrulesfile) throws IOException {
		BufferedWriter writer = new BufferedWriter(new FileWriter(folderlocation + "SampleOut.txt"));
        
		FC = new fastaCombine(folderlocation + outputfilename, folderlocation+ "aminomassindexes.txt", folderlocation + splitrulesfile, 3);
		FC.Create();

        //need a writer to test structure and output
        
        Iterator<Segment> iter = FC.getSegmentList().iterator();
        Segment tempSeg;
        long time = System.currentTimeMillis();
        
        while(iter.hasNext()){
            tempSeg = iter.next();
            DecimalFormat dF = new DecimalFormat();
            dF.applyPattern("00000.00000");
            writer.write(dF.format(tempSeg.getMass()) + "\t" +tempSeg.getBZUncertanties() + "\t" + tempSeg.getLength()+ "\t"+ tempSeg.getSequence()  + "\r\n");
        }
        System.out.println("number of Proteins: " + FC.getPeptideList().size());
        
        System.out.println("size of total singleSegments: " + FC.getSingleList().size());
        System.out.println("size of total ComboSegments: " + FC.getComboList().size());       
        System.out.println("size of total Segment List: " + FC.getSegmentList().size());
        System.out.println("Time Write Taken: " + (System.currentTimeMillis()-time));
        writer.close();
                
	}
	
}